Skip to content

fix(sql): reject duplicate unqualified names in CTAS, CREATE VIEW, and SELECT INTO#22290

Merged
Dandandan merged 2 commits into
apache:mainfrom
kumarUjjawal:fix/sql_duplicate_unqualified_table
May 19, 2026
Merged

fix(sql): reject duplicate unqualified names in CTAS, CREATE VIEW, and SELECT INTO#22290
Dandandan merged 2 commits into
apache:mainfrom
kumarUjjawal:fix/sql_duplicate_unqualified_table

Conversation

@kumarUjjawal

@kumarUjjawal kumarUjjawal commented May 17, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

CREATE TABLE AS, CREATE VIEW, and SELECT INTO can persist only plain
column names.

Before this change, a query like a self join could produce qualified output
columns such as x.column1 and y.column1, which are valid while planning.
But when the table or view was created, those qualifiers were dropped and the
stored schema became column1, column1, ... with duplicates.

That meant the create step succeeded, but the created table or view failed
later when the user tried to read from it. The object existed in the catalog
but was not usable.

This PR fixes that by rejecting these cases during execution of the DDL
operators, before the table or view is registered.

What changes are included in this PR?

This PR adds an execution-side check that validates the final output schema has
unique unqualified column names.

It applies that check in:

  • create_memory_table
  • create_view

This covers:

  • CREATE TABLE AS
  • CREATE VIEW
  • SELECT INTO

The check runs after optimization or type coercion and before registration, so
it validates the actual names that would be stored.

For IF NOT EXISTS, execution keeps the existing no-op behavior when the
target already exists, so validation only runs when a new object would
actually be created or replaced.

This PR also adds:

  • sqllogictest coverage for the duplicate-column self-join case for all three flows
  • direct execution-level regression tests for create / replace table and view paths

Are these changes tested?

Yes

Are there any user-facing changes?

Invalid CREATE TABLE AS, CREATE VIEW, and SELECT INTO statements that
would create an object with duplicate unqualified column names now fail early
during DDL execution with a schema error, instead of succeeding and creating
an unreadable table or view.

No API Changes

@github-actions github-actions Bot added sql SQL Planner sqllogictest SQL Logic Tests (.slt) labels May 17, 2026

@nuno-faria nuno-faria left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kumarUjjawal. I wonder if it would be better to check this when executing the operators, i.e., in create_memory_table and create_view.

@kumarUjjawal

Copy link
Copy Markdown
Contributor Author

Thank you @nuno-faria I agree with the approach of execution time check. I will see how the code looks and rework this.

@kumarUjjawal kumarUjjawal requested a review from nuno-faria May 19, 2026 04:49
@github-actions github-actions Bot added core Core DataFusion crate and removed sql SQL Planner labels May 19, 2026

@nuno-faria nuno-faria left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kumarUjjawal, LGTM.

@kumarUjjawal

Copy link
Copy Markdown
Contributor Author

Thank you @nuno-faria for the review

@kumarUjjawal kumarUjjawal added this pull request to the merge queue May 19, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks May 19, 2026
@Dandandan Dandandan added this pull request to the merge queue May 19, 2026
Merged via the queue into apache:main with commit e960c4b May 19, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CREATE TABLE AS not checking column unicity

3 participants